package com.yisin.www.chat;

import java.net.Socket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class PushThread implements Runnable {

	private Socket soc;
	
	public PushThread(Socket soc){
		this.soc = soc;
	}
	
	public void run() {
		Collection<Map<String, String>> userInfo = ChatServlet.getChat().userMap.values();
		Map<String, Object> msgMap = new HashMap<String, Object>();
		Iterator<Map<String, String>> ite = userInfo.iterator();
		List<Map<String, String>> users = new ArrayList<Map<String, String>>();
		int size = 0;
		while(ite.hasNext()){
			users.add(ite.next());
			size++;
			if(size > 6){
				msgMap.put("genre", 6);// 推送用户列表
				msgMap.put("userList", users);
				ChatServlet.getChat().sendMsgToClient(soc, ChatServlet.getChat().mapToJsonString(msgMap));
				size = 0;
				users = new ArrayList<Map<String, String>>();
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		if(users.size() > 0){
			msgMap.put("genre", 6);// 推送用户列表
			msgMap.put("userList", users);
			ChatServlet.getChat().sendMsgToClient(soc, ChatServlet.getChat().mapToJsonString(msgMap));
		}
	}

}
